clear
est clear

********************************************************************************
* quitline

import excel using "rawdata\Quitline_to2021.xlsx", clear sheet("Incoming Calls") firstrow case(lower)

ren locationdescription state_name
keep if variable == "Total Incoming Calls"
drop variable
ren value calls

tempfile temp_calls_int1
sa `temp_calls_int1'

keep if inrange(year, 2010, 2018)

g bin = 1 if daterange == "Jan-Mar" | daterange == "Apr-Jun"
replace bin = 2 if daterange == "Jul-Sep" | daterange == "Oct-Dec"
gen quarter = 1

collapse (sum) calls quarter, by(state year bin) fast
drop if quarter == 1
drop quarter

tempfile temp_calls_int2
sa `temp_calls_int2'

u `temp_calls_int1', clear

keep if inrange(year, 2019, 2020)

g bin = 1 if daterange == "Jan-Jun"
replace bin = 2 if daterange == "Jul-Dec"

collapse (sum) calls, by(state year bin) fast

app using `temp_calls_int2'

loc nums "2 1 5 4 6 8 9 11 10 12 13 15 19 16 17 18 20 21 22 25 24 23 26 27 29 28 30 37 38 31 33 34 35 32 36 39 40 41 42 44 45 46 47 48 49 51 50 53 55 54 56"
loc names ""Alaska" "Alabama" "Arkansas" "Arizona" "California" "Colorado" "Connecticut" "District of Columbia" "Delaware" "Florida" "Georgia" "Hawaii" "Iowa" "Idaho" "Illinois" "Indiana" "Kansas" "Kentucky" "Louisiana" "Massachusetts" "Maryland" "Maine" "Michigan" "Minnesota" "Missouri" "Mississippi" "Montana" "North Carolina" "North Dakota" "Nebraska" "New Hampshire" "New Jersey" "New Mexico" "Nevada" "New York" "Ohio" "Oklahoma" "Oregon" "Pennsylvania" "Rhode Island" "South Carolina" "South Dakota" "Tennessee" "Texas" "Utah" "Virginia" "Vermont" "Washington" "Wisconsin" "West Virginia" "Wyoming""

g fips = .
for zz in any `nums' \ yy in any `names': replace fips = zz if state_name == "yy"

tempfile quitline
save `quitline', replace

********************************************************************************
* county core

u "rawdata/county_core", clear

keep if inrange(year, 2010, 2020)

* indoor cigarette bans
g cig_ban = cig_TreatEfW_cnty/3 + cig_TreatEfR_cnty/3 + cig_TreatEfB_cnty/3
g ecig_ban = ecig_TreatEfW_cnty/3 + ecig_TreatEfR_cnty/3 + ecig_TreatEfB_cnty/3

ren state fips

g pop_vary = demo_popadult_cnty + demo_popkid_cnty

keep cig_tax_cnty ecig_taxstand2cons_y20_cty tob21_cnty econ_unemploy_cnty pop demo_popadult_cnty demo_popkid_cnty demo_povall_cnty demo_income_cnty cig_ban ecig_ban fips county year quarter demo_urban_cnty pop_vary ecig_taxlowdist_cnty ecig_min_age_cnty ecig_tax_adval_cty ecig_tax_excise_cty ecig_tax_sales_cty ecig_tax_cont_cty ecig_tax_twotier_cty

recode demo_urban_cnty (1 2=1) (3/6=0) (.=.), g(urban)
recode demo_urban_cnty (1 2=0) (3/6=1) (.=.), g(rural)

loc vars1 "cig_tax_cnty ecig_taxstand2cons_y20_cty tob21_cnty econ_unemploy_cnty demo_popadult_cnty demo_popkid_cnty demo_povall_cnty demo_income_cnty ecig_min_age_cnty"
loc vars2 "cig_tax_county ecig_tax_county tob21 uerate adults kids proverty income ecig_mlsa"
for zz in any `vars1' \ yy in any `vars2': ren zz yy

recode quarter (1 2=1) (3 4=2), g(bin)

collapse (mean) cig_tax_county ecig_tax_county tob21 uerate adults kids proverty income cig_ban ecig_ban rural urban ecig_taxlowdist_cnty ecig_mlsa ecig_tax_adval_cty ecig_tax_excise_cty ecig_tax_sales_cty ecig_tax_cont_cty ecig_tax_twotier_cty (rawsum) pop_vary [aw=pop_vary] , by(fips year bin) fast

replace ecig_mlsa = 1 if ecig_mlsa == .

* create time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22"
loc nums2 "2010 2010 2011 2011 2012 2012 2013 2013 2014 2014 2015 2015 2016 2016 2017 2017 2018 2018 2019 2019 2020 2020"
loc nums3 "1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & bin == xx

* event study indicators
so fips time
g eff_time = time if ecig_tax_county != 0 & ecig_tax_county[_n-1] == 0 & fips == fips[_n-1]
replace eff_time = eff_time[_n-1] if eff_time[_n-1] != . & fips == fips[_n-1]
gsort fips -time
replace eff_time = eff_time[_n-1] if eff_time[_n-1] != . & fips == fips[_n-1]
so fips time

* relative time
g rel_time = time-eff_time

* indicators, -4 to +4
recode rel_time (-100/-4=1) (-3/100 .=0), g(time_m4)
recode rel_time (-3=1) (-100/-4 -2/100 .=0), g(time_m3)
recode rel_time (-2=1) (-100/-3 -1/100 .=0), g(time_m2)
recode rel_time (-1=1) (-100/-2 0/100 .=0), g(time_m1)
recode rel_time (0=1) (-100/-1 1/100 .=0), g(time_p0)
recode rel_time (1=1) (-100/0 2/100 .=0), g(time_p1)
recode rel_time (2=1) (-100/1 3/100 .=0), g(time_p2)
recode rel_time (3=1) (-100/2 4/100 .=0), g(time_p3)
recode rel_time (4/100=1) (-100/3 .=0), g(time_p4)

loc vars "time_m4 time_m3 time_m2 time_m1 time_p0 time_p1 time_p2 time_p3 time_p4"
loc labs ""-4" "-3" "-2" "-1" "0" "+1" "+2" "+3" "+4""
for zz in any `vars' \ yy in any `labs': lab var zz "yy"

egen max = max(rel_time), by(fips)
egen min = min(rel_time), by(fips)

g balance_down = 0
replace balance_down = 1 if inrange(min, -42, -4)
g balance_up = 0
replace balance_up = 1 if inrange(max, 4, 41)

drop min max
drop time

g state = "."
loc names "AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY"
loc nums "2 1 5 4 6 8 9 11 10 12 13 15 19 16 17 18 20 21 22 25 24 23 26 27 29 28 30 37 38 31 33 34 35 32 36 39 40 41 42 44 45 46 47 48 49 51 50 53 55 54 56"
for zz in any `names' \ yy in any `nums': replace state = "zz" if fips == yy

tempfile county
save `county', replace

********************************************************************************
* state core

u "rawdata/state_core", clear

g demo_male_st = 1 - demo_female_st

keep if inrange(year, 2010, 2020)

* convert the indoor vaping and smoking bans to any FULL ban
loc vars1 "cig_workplace_restrict_st cig_restaurant_restrict_st cig_bar_restrict_st ecig_workplace_restrict_st ecig_restaurant_restrict_st ecig_bar_restrict_st"
loc vars2 "cig_work_ban cig_rest_ban cig_bar_ban ecig_work_ban ecig_rest_ban ecig_bar_ban"
for zz in any `vars1' \ yy in any `vars2': recode zz (2=1) (0 1=0), g(yy)

loc names ""Cigarette ban at worksites" "Cigarette ban at bars" "Cigarette ban at restaurants" "E-cigarette ban at worksites" "E-cigarette ban at bars" "E-cigarette ban at restaurants""
for zz in any `vars2' \ yy in any `names': lab var zz "yy"

recode ecig_lic_st (0=0) (1/4=1), g(ecig_lic)
recode tob_lic_st (0=0) (1/4=1), g(tob_lic)
drop ecig_lic_st tob_lic_st

ren alc_beer_tax_st beer_tax_st
ren alc_liquor_tax_st liq_tax_st
ren state fips
ren ACA medicaid_expansion
ren econ_cpi cpi
ren econ_unemploy_st unemployrate
ren ecig_tax_excise_st ecig_tax_excise
ren ecig_tax_percent_st e_cig_percent

* recode the Medicaid coverage of the services, coverage must be full (so yes)
loc mcaid_vars "mcaid_cig_gum_ffs mcaid_cig_gum_mc mcaid_cig_lozenge_ffs mcaid_cig_lozenge_mc mcaid_cig_patch_ffs mcaid_cig_patch_mc"
for zz in any `mcaid_vars': recode zz (1 = 1) (0 2 3/5=0), g(zz_t)

* cover product in FFS or MC Medicaid (some states do not have both programs)
egen mcaid_gum = rowmax(mcaid_cig_gum_ffs_t mcaid_cig_gum_mc_t)
lab var mcaid_gum "Medicaid covers NRT gum in FFS or MC"
egen mcaid_loz = rowmax(mcaid_cig_lozenge_ffs_t mcaid_cig_lozenge_mc_t)
lab var mcaid_loz "Medicaid covers NRT lozenge in FFR or MC"
egen mcaid_patch = rowmax(mcaid_cig_patch_ffs_t mcaid_cig_patch_mc_t)
lab var mcaid_patch "Medicaid covers NRT patch in FFS or MC"
drop *_t

* combine the temporary and permanment e-cigarette bans into one variable
egen ban_ecigs = rowmax( ecig_tempban_st ecig_permban_st)
lab var ban_ecigs "Temporary or permanent e-cigarettte ban"

* locate first cigarette tax increase in the panel, this is now the first increase that is 50 cents or more, not defined if there was no such hike
g cig_tax_st_nom = cig_tax_st
recode quarter (1 2=1) (3 4=2), g(bin)

collapse (mean) cig_tax_st cig_tax_fed cig_workplace_restrict_st cig_restaurant_restrict_st cig_bar_restrict_st cig_min_age_st ecig_tax_excise e_cig_percent ecig_workplace_restrict_st ecig_restaurant_restrict_st ecig_bar_restrict_st ecig_tempban_st ecig_permban_st mcaid_cig_gum_ffs mcaid_cig_gum_mc mcaid_cig_ind_ffs mcaid_cig_ind_mc mcaid_cig_lozenge_ffs mcaid_cig_lozenge_mc mcaid_cig_patch_ffs mcaid_cig_patch_mc tob_cigar_tax_st tob_littlecigar_tax_st tob_cigar_tax_percent_st tob_littlecigar_tax_percent_st tob_pipe_tax_percent_st tob_roll_tax_percent_st tob_cigar_cap tob_littlecigar_cap opioid_p_pdmp_st marij_mml_st marij_receffect_st medicaid_expansion beer_tax_st liq_tax_st cpi unemployrate demo_govdem_st demo_pov_st demo_uninsured_st demo_hispanic_st demo_white_st demo_black_st demo_AIAN_st demo_API_st demo_othmult_st demo_edu_underHS_st demo_edu_HS_st demo_edu_somecoll_st demo_edu_coll_st demo_edu_grad_st demo_female_st demo_age_st demo_male_st cig_work_ban cig_rest_ban cig_bar_ban ecig_work_ban ecig_rest_ban ecig_bar_ban ecig_lic tob_lic mcaid_gum mcaid_loz mcaid_patch ban_ecigs cig_tax_st_nom, by(fips year bin) fast

* create a time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22"
loc nums2 "2010 2010 2011 2011 2012 2012 2013 2013 2014 2014 2015 2015 2016 2016 2017 2017 2018 2018 2019 2019 2020 2020"
loc nums3 "1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & bin == xx

so fips year bin
g dif_cig = cig_tax_st_nom - cig_tax_st_nom[_n-1] if fips == fips[_n-1]
g cig_tax_inc = 0
replace cig_tax_inc = 1 if dif_cig >= 0.50 & dif_cig != .

g test = time if cig_tax_inc == 1
egen eff_time_cig_tax = min(test), by(fips)
drop test cig_tax_inc
lab var eff_time_cig_tax "Time of first nominal state cigarette tax change of 50 cents or more (following JRU)"

* cigaratte tax increase relative time
g rel_time_cig_tax = time - eff_time_cig_tax

recode rel_time_cig_tax (-100/-4=1) (-3/100 .=0), g(time_c_m4)
recode rel_time_cig_tax (-3=1) (-100/-4 -2/100 .=0), g(time_c_m3)
recode rel_time_cig_tax (-2=1) (-100/-3 -1/100 .=0), g(time_c_m2)
recode rel_time_cig_tax (-1=1) (-100/-2 0/100 .=0), g(time_c_m1)
recode rel_time_cig_tax (0=1) (-100/-1 1/100 .=0), g(time_c_p0)
recode rel_time_cig_tax (1=1) (-100/0 2/100 .=0), g(time_c_p1)
recode rel_time_cig_tax (2=1) (-100/1 3/100 .=0), g(time_c_p2)
recode rel_time_cig_tax (3=1) (-100/2 4/100 .=0), g(time_c_p3)
recode rel_time_cig_tax (4/100=1) (-100/3 .=0), g(time_c_p4)

loc vars "time_c_m4 time_c_m3 time_c_m2 time_c_m1 time_c_p0 time_c_p1 time_c_p2 time_c_p3 time_c_p4"
loc labs ""-4" "-3" "-2" "-1" "0" "+1" "+2" "+3" "+4""
for zz in any `vars' \ yy in any `labs': lab var zz "yy"

egen max = max(rel_time_cig_tax), by(fips)
replace max = . if eff_time_cig_tax == .

egen min = min(rel_time_cig_tax), by(fips)

g balance_down_cig = 0
replace balance_down_cig = 1 if inrange(min, -42, -4)
g balance_up_cig = 0
replace balance_up_cig = 1 if inrange(max, 4, 41)

drop min max
drop time

tempfile state
sa `state', replace

********************************************************************************
* merge

u `quitline', clear
mer 1:1 fips year bin using `county', nogen keep(3)
mer 1:1 fips year bin using `state', nogen keep(3)

replace calls = calls/pop*100000

* create the full cigarette tax, sum of federal, state, and local taxes
egen cig_tax = rowtotal(cig_tax_st cig_tax_fed cig_tax_county)
lab var cig_tax "Total cigarette tax (county, state, and federal)"

* convert financial variables from nominal to real terms
su cpi if year == 2020 & bin == 2
g base = r(mean)
g cpi20 = cpi/base

loc nom_val "cig_tax_county ecig_tax_county cig_tax_st cig_tax_fed ecig_tax_excise tob_cigar_tax_st tob_littlecigar_tax_st beer_tax cig_tax income beer_tax_st liq_tax_st"
for zz in any `nom_val': replace zz = zz/cpi20

* create time variable
loc nums1 "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22"
loc nums2 "2010 2010 2011 2011 2012 2012 2013 2013 2014 2014 2015 2015 2016 2016 2017 2017 2018 2018 2019 2019 2020 2020"
loc nums3 "1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2"

g time = .
for zz in any `nums1' \ yy in any `nums2' \ xx in any `nums3': replace time = zz if year == yy & bin == xx

sa "workingdata/quitline_tax_clean", replace

